<?php
namespace app\Manage\controller;

use app\common\controller\Manage;
use think\facade\Request;
use app\common\model\Dept as DeptModel;
use app\common\model\User as UserModel;
use app\common\model\Manage as ManageModel;
use think\facade\Log;
use think\Db;

class Dept extends Manage
{
    public function index()
    {
        if(Request::isAjax()){
            $deptModel = new DeptModel();
            return $deptModel->tableData(input('param.'));
        }else{
            return $this->fetch('index');
        }
    }

    public function getList()
    {
        $return_data = [
            'code' => 0,
            'msg'  => '获取成功',
            'data' => [],
        ];

        $deptModel = new DeptModel();
        $re              = $deptModel->getList(input('param.id/d'));
        if (!$re['status']) {
            return $re;
        }
        
        $return_data['data'] = $re['data'];
        return $return_data;
    }
    
    /**
     *  部门分类添加
     * @return array|mixed
     */
    public function add()
    {
        $this->view->engine->layout(false);
        if(Request::isPost())
        {
            $deptModel = new DeptModel();
            return $deptModel->addData(input('param.'));
        }
        $data = [];
        $data['pid']= input('param.')['pid'];
        $data['pname']= input('param.')['pname'];

        return $this->fetch('add',['data' => $data]);
    }

    /**
     *  部门分类修改
     * @return array|mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function edit()
    {
        $this->view->engine->layout(false);
        $deptModel = new DeptModel();
        if(Request::isPost())
        {
            return $deptModel->saveData(input('param.'));
        }
        $data = $deptModel->where('id',input('param.id'))->find();
        if (!$data) {
            return error_code(10002);
        }
        
        $data['pid']= $data['parent_id'];
        //$data['pid']= input('param.')['pid'];
        $data['pname']= input('param.')['pname'];

        return $this->fetch('edit',['data' => $data]);
    }

    /**
     *  部门 软删除
     * User:tony
     * @return array
     */
    public function del()
    {
        $result = [
            'status' => false,
            'msg' => '删除失败',
            'data' => []
        ];

        $id  = input('post.id', '');

        if (!$id) {
            return $result;
        }
 
        $userModel = new UserModel();
        $used = $userModel->where('deptId', $id)->find();

        if ($used) { 
            $result['msg']    = '当前部门下存在用户无法删除！';
            return $result;
        }

        $manageModel = new ManageModel();
        $manage = $manageModel->where('deptId', $id)->find();
        if ($manage) { 
            $result['msg']    = '当前部门下存在管理员无法删除！';
            return $result;
        }

        $deptModel = new DeptModel();
        $hasChild = $deptModel->where('parent_id', $id)->find();
        if ($hasChild) { 
            $result['msg']    = '当前部门下存在子部门！';
            return $result;
        }
 
        $deptModel->where(['id'=>$id])->delete();

        $result['status'] = true;
        $result['msg'] = '删除成功';
        
        return $result;
    }
}
